package com.bocai.dahuatech.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.ProductListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.ProductListAdapterHolder;
import com.bocai.dahuatech.ui.pressent.MyorderPressent;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.KeyBoardUtils;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 产品搜索
 */
public class SearchProductActivity extends BaseActivity implements MyorderPressent{
    @Bind(R.id.swipe_search)
    SwipeRefreshLayout swipeSearch;
    @Bind(R.id.lv_search_order)
    LoadMoreRecyclerView lvSearchOrder;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.tv_title_right)
    TextView tvRight;
    ProductListBean productListBean;
    ProductListAdapterHolder adapter;
    ArrayList<ProductListBean.ContentEntity> datas=new ArrayList<>();
    int page=1;
    int what;
    String key="";
    @Override
    protected int contentView() {
        return R.layout.activity_search_myorder;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.serchproduct);
        etSearch.setHint(R.string.productserchhint);
        what=getIntent().getIntExtra("what",0);
        key=getIntent().getStringExtra("key");
        if(what==1){
            getData(key);
        }
        lvSearchOrder.setLayoutManager(new LinearLayoutManager(this));
        initEvent();
    }


    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("goshopcart"))
        {
            finish();
        }
    }
    private void getData(String key) {
        showLoading();
        getTestApi().getProductList(0,0,"",key,page,100)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            swipeSearch.setRefreshing(false);
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error", "zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            swipeSearch.setRefreshing(false);
            hideLoading();
            if (baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                productListBean=new Gson().fromJson(result,ProductListBean.class);
                setDatas();
                if(productListBean.getContent().size()==0){
                    showToast(R.string.noserchproduct);
                }
            }
        }
    };

    private void setDatas() {
        adapter=new ProductListAdapterHolder(this,productListBean.getContent());
        lvSearchOrder.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData(etSearch.getText().toString());
                KeyBoardUtils.closeKeybord(etSearch, SearchProductActivity.this);
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    getData(etSearch.getText().toString());
                    KeyBoardUtils.closeKeybord(etSearch, SearchProductActivity.this);
                    key=etSearch.getText().toString();
                }
                return false;
            }
        });
        swipeSearch.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                datas.clear();
                getData(key);
            }
        });
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyBoardUtils.closeKeybord(etSearch,SearchProductActivity.this);
                finish();
            }
        });
    }

    @Override
    public void setIsShow(boolean isShow) {

    }
}
